package com.lex.crossgate.admin.modules.sys.service.imp;

import com.lex.crossgate.common.constants.Constant;
import com.lex.crossgate.common.constants.R;;
import com.lex.crossgate.admin.modules.sys.dao.SysUserRoleDao;
import com.lex.crossgate.admin.modules.sys.entity.SysUserRole;
import com.lex.crossgate.admin.modules.sys.service.SysUserRoleService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * java类简单作用描述
 *
 * @ProjectName: crossgate
 * @Package: com.lex.crossgate.admin.modules.sys.service.imp
 * @ClassName: ${TYPE_NAME}
 * @Description: java类作用描述
 * @Author: 李贝留斯
 * @CreateDate: 2018/5/2 15:55
 * @Version: 1.0
 **/
@Service("sysUserRoleService")
public class SysUserRoleServiceImpl implements SysUserRoleService {

    @Autowired
    private SysUserRoleDao sysUserRoleDao;

    /**
     * @description: 保存权限
     * @author 李贝留斯
     * @date 2018/5/17 20:33
     */
    @Override
    @Transactional
    public R save(String userId,String roleId) {
        //删除次用户权限
        EntityWrapper entityWrapper = new EntityWrapper();
        if(StringUtils.isNotEmpty(userId))
            entityWrapper.eq("user_id",userId);
        sysUserRoleDao.delete(entityWrapper);
        //重新保存用户权限
        int rows = 0;
        if(StringUtils.isNotEmpty(roleId)){
            String[] roleIds = roleId.split(",");
            for (String urId : roleIds){
                SysUserRole sysUserRole = new SysUserRole();
                sysUserRole.setRoleId(urId);
                sysUserRole.setUserId(userId);
                int row = sysUserRoleDao.insert(sysUserRole);
                rows+= row;
            }
        }
        return R.ok(Constant.ERROR);

    }

    public List<SysUserRole> getUserRoleById(String userId) {
        EntityWrapper entityWrapper = new EntityWrapper();
        entityWrapper.eq("user_id",userId);
        return sysUserRoleDao.selectList(entityWrapper);
    }
}
